﻿<script type="text/javascript">
    $(function () {
        getButton("menu");
        
    });
    function ui_menu_init_list(toolbar) {
        $("#ui_menu_tg").treegrid({       //初始化datagrid
            url: "ashx/rm_menu.ashx?action=getall",
            idField: 'id',
            treeField: 'text',
            rownumbers: true,
            frozenColumns: [[                             
                             { field: 'text', title: '菜单名称', width: 200 }
            ]],
            columns: [[
                       { field: 'code', title: '标识码', width: 120 },
                       { field: 'url', title: '链接地址', width: 200 },
                       { field: 'iconCls', title: '图标', width: 150 },
                       { field: 'sort', title: '排序', width: 80 },                       
                       { field: 'addDate', title: '添加时间', width: 130 }
            ]],
            toolbar: toolbar.length == 0 ? null : toolbar,
            //toolbar: [{
            //    text: '添加',
            //    iconCls: 'icon-add',
            //    handler: function () { ui_menu_add(); }
            //}, {
            //    text: '修改',
            //    iconCls: 'icon-edit',
            //    handler: function () { ui_menu_edit(); }
            //},{
            //    text: '删除',
            //    iconCls: 'icon-remove',
            //    handler: function () { ui_menu_delete() }
            //}, '-', {
            //text: '分配按钮',
            //iconCls: 'icon-link',
            //handler: function () { ui_menu_setbutton() }
            //}, '-', {
            //    text: '全部展开',
            //    iconCls: 'icon-expand',
            //    handler: function () { ui_menu_expandall(); }
            //}, {
            //    text: '全部隐藏',
            //    iconCls: 'icon-collapse',
            //    handler: function () { ui_menu_collapseall(); }
            //}],
            onDblClickRow: function (rowIndex, rowData) {     //双击行弹框编辑
                ui_menu_edit();
            }
        });
    }

    function ui_menu_add() {
        $("<div/>").dialog({
            id: "ui_menu_add_dialog",
            href: "html/ui_menu_edit.html",
            title: "添加菜单",
            iconCls: 'icon-add',
            height: 350,
            width: 460,
            modal: true,
            buttons: [{
                id: "ui_menu_add_btn",
                text: '添 加',
                iconCls: 'icon-add',
                handler: function () {
                    $("#ui_menu_editform").form("submit", {
                        url: "ashx/rm_menu.ashx",
                        onSubmit: function (param) {
                            $('#ui_menu_add_btn').linkbutton('disable');    //点击就禁用按钮，防止连击
                            param.action = 'add';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_menu_add_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse( data );              //转成json格式
                            if (dataJson.success) {
                                $("#ui_menu_add_dialog").dialog('destroy'); //销毁dialog对象
                                $.show_warning("提示", "添加成功！");
                                $("#ui_menu_tg").treegrid("reload");
                            } else {
                                $('#ui_menu_add_btn').linkbutton('enable');   //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_menu_name_add").focus();
            },
            onClose: function () {
                $("#ui_menu_add_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_menu_edit() {
        //$.show_warning("提示", "菜单编辑");
        var row = $("#ui_menu_tg").treegrid("getSelected");
        if (!row) {
            $.show_warning("提示", "请先勾选要删除的菜单");
            return;
        }
        
        $("<div/>").dialog({
            id: "ui_menu_edit_dialog",
            href: "html/ui_menu_edit.html",
            title: "修改菜单",
            height: 350,
            width: 460,
            modal: true,
            buttons: [{
                id: "ui_menu_edit_btn",
                text: '修 改',
                handler: function () {
                    $("#ui_menu_editform").form("submit", {
                        url: "ashx/rm_menu.ashx",
                        onSubmit: function (param) {
                            $('#ui_menu_edit_btn').linkbutton('disable');   //点击就禁用按钮，防止连击
                            param.action = 'edit';
                            if ($(this).form('validate'))
                                return true;
                            else {
                                $('#ui_menu_edit_btn').linkbutton('enable');   //恢复按钮
                                return false;
                            }
                        },
                        success: function (data) {
                            var dataJson = JSON.parse(data);                    //转成json格式
                            if (dataJson.success) {
                                $("#ui_menu_edit_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "修改成功！");
                                $("#ui_menu_tg").treegrid("reload");
                            } else {
                                $('#ui_menu_edit_btn').linkbutton('enable');    //恢复按钮
                                $.show_warning("提示", dataJson.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {
                $("#ui_menu_id_edit").val(row.id);
                $("#ui_menu_name_edit").textbox("setValue",row.text);
                $('#ui_menu_sort_edit').numberspinner('setValue', row.sort);
                $("#ui_menu_code_edit").textbox("setValue", row.code);
                $("#ui_menu_linkaddress_edit").textbox("setValue", row.url);
                $("#ui_menu_icon_edit").textbox("setValue", row.iconCls);
                if (row.parentId > 0)
                    $("#ui_menu_parentid_edit").combotree('setValue', row.parentId);
            },
            onClose: function () {
                $("#ui_menu_edit_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_menu_delete() {
        var rows = $("#ui_menu_tg").treegrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要删除的菜单");
            return;
        }
        $.messager.confirm('提示', '确定删除勾选的这' + rows.length + '个菜单？如果有子菜单也会同时删除！', function (r) {
            if (r) {
                para = {};
                para.action = "delete";
                para.timespan = new Date().getTime();
               
                var ids = [];
                $.each(rows, function (i, row) {
                    ids.push(row.id);
                });
                para.ids = ids.join(',');
                $.ajax({
                    url: "ashx/rm_menu.ashx",
                    data: para,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            $.show_warning("提示", "删除成功！");
                            $("#ui_menu_tg").treegrid("reload").treegrid('clearSelections').treegrid('clearChecked');
                        } else {
                            $.show_warning("提示", data.msg);
                        }
                    }
                });
            }
        });
    }
    function ui_menu_setbutton() {
        var rows = $("#ui_menu_tg").treegrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要分配按钮的菜单");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "无法批量分配菜单对应的按钮");
            $("#ui_menu_tg").treegrid('clearSelections').treegrid('clearChecked');
            return;
        }
        var row = rows[0];
        if (row.parentId == 0 || row.parentId == "") {
            $.show_warning("提示", "根节点无法分配按钮");
            $("#ui_menu_tg").treegrid('clearSelections').treegrid('clearChecked');
            return;
        }
        //$.show_warning("提示", "菜单分配按钮");
        //初始化dialog
        $("<div/>").dialog({
            id: "ui_menu_setbutton_dialog",
            href: "html/ui_menu_setbutton.html",
            title: "按钮分配：" + row.text,
            height: 400,
            width: 400,
            modal: true,
            buttons: [{
                id: "ui_menu_setbutton_btn",
                text: '分配',
                handler: function () {
                    $('#ui_menu_setbutton_btn').linkbutton('disable');
                    setbutton(row.id);  //为菜单分配按钮
                }
            }],
            onLoad: function () {
                $("#ui_menu_setbutton_menuname").val(row.text);
                $("#ui_menu_setbutton_menuid").val(row.id);
                $('#ui_menu_setbutton_button').combotree("setValues",row.buttonIds);
            },
            onClose: function () {
                $("#ui_menu_setbutton_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }

    function setbutton(menuId) {
        var buttonIds = [];
        
        var nodes = $('#ui_menu_setbutton_button').combotree("tree").tree('getChecked');        
        for (var i = 0; i < nodes.length; i++) {
            buttonIds.push(nodes[i].id );
            //最终拼接示例：1,2,11 按钮id列表
        }
        var para = {};
        para.action = "setbutton";
        para.timespan = new Date().getTime();
        para.buttonIds = buttonIds.join(',');
        para.menuId = menuId;

        $.ajax({
            url: "ashx/rm_menu.ashx",
            data: para,
            type: "POST",
            dataType: "json",
            success: function (data) {
                if (data.success) {
                    $("#ui_menu_setbutton_dialog").dialog('destroy');  //销毁dialog对象
                    $.show_warning("提示", "分配成功！");
                    $("#ui_menu_tg").treegrid("reload").treegrid('clearSelections').treegrid('clearChecked');
                } else {
                    $('#ui_menu_setbutton_btn').linkbutton('enable');
                    $.show_warning("提示", data.msg);
                }
            }
        });
    }
    function ui_menu_expandall() {
        $("#ui_menu_tg").treegrid("expandAll");
    }
    function ui_menu_collapseall() {
        $("#ui_menu_tg").treegrid("collapseAll");
    }
</script>
<div id="ui_menu_layout" class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:false">
        <table id="ui_menu_tg" data-options="fit:true,border:false">
        </table>
    </div>
</div>
